<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标题字段" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题字段"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否乡音推荐" prop="recommendIs">
        <el-input
          v-model="queryParams.recommendIs"
          placeholder="请输入是否乡音推荐"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对应详情类型" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择对应详情类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in categoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="列表标签" prop="label">
        <el-select
          v-model="queryParams.label"
          placeholder="请选择列表标签"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in labelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类标签" prop="secondLabel">
        <el-select
          v-model="queryParams.secondLabel"
          placeholder="请选择分类标签"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in secondLabelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['view:comprehensive_display:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['view:comprehensive_display:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['view:comprehensive_display:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['view:comprehensive_display:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="comprehensive_displayList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="标题字段" align="center" prop="title" />
      <el-table-column label="图片地址" align="center" prop="imagesUrl">
        <template slot-scope="scope">
          <a
            :href="scope.row.imagesUrl"
            target="_blank"
            v-if="scope.row.imagesUrl"
          >
            <img class="Image" :src="scope.row.imagesUrl" alt />
          </a>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="introduction" :show-overflow-tooltip="true"/>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="是否乡音推荐" align="center" prop="recommendIs">
        <template slot-scope="scope">
          <el-popconfirm
            title="是否确定推荐"
            v-if="!scope.row.recommendIs"
            @confirm="toSets(scope.row, true)"
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              slot="reference"
              >设置乡音推荐</el-button
            >
          </el-popconfirm>
          <el-popconfirm
            title="是否确定取消"
            v-else
            @confirm="toSets(scope.row, false)"
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              slot="reference"
              >取消</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column
        label="对应详情类型"
        align="center"
        prop="category"
        :formatter="categoryFormat"
      />
      <el-table-column label="排序字段" align="center" prop="sort" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['view:comprehensive_display:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['view:comprehensive_display:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改综合展示对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题字段" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题字段" />
        </el-form-item>
        <el-form-item label="图片">
          <imageUpload v-model="form.imagesUrl" />
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input
            type="textarea"
            v-model="form.introduction"
            placeholder="请输入简介"
          />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="对应详情类型" prop="category">
          <el-select v-model="form.category" placeholder="请选择对应详情类型">
            <el-option
              v-for="dict in categoryOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序字段" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序字段" />
        </el-form-item>
        <el-form-item label="列表标签" prop="label">
          <el-select v-model="form.label" multiple placeholder="请选择列表标签">
            <el-option
              v-for="dict in labelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类标签" prop="secondLabel">
          <el-select
            v-model="form.secondLabel"
            multiple
            placeholder="请选择列表标签"
          >
            <el-option
              v-for="dict in secondLabelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listComprehensive_display,
  getComprehensive_display,
  delComprehensive_display,
  addComprehensive_display,
  updateComprehensive_display,
  exportComprehensive_display,
} from "@/api/view/comprehensive_display";
import ImageUpload from "@/components/ImageUpload";
import Editor from "@/components/Editor";

export default {
  name: "Comprehensive_display",
  components: {
    ImageUpload,
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 综合展示表格数据
      comprehensive_displayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 对应详情类型字典
      categoryOptions: [],
      // 列表标签字典
      labelOptions: [],
      // 分类标签字典
      secondLabelOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        imagesUrl: null,
        introduction: null,
        address: null,
        recommendIs: null,
        selectedIs: null,
        category: null,
        sort: null,
        detailed: null,
        label: null,
        phoneNumber: null,
        secondLabel: null,
        consumptionPerPerson: null,
        businessHours: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题字段不能为空", trigger: "blur" },
        ],
        imagesUrl: [
          { required: true, message: "图片地址不能为空", trigger: "blur" },
        ],
        category: [
          {
            required: true,
            message: "对应详情类型不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("detail_type").then((response) => {
      this.categoryOptions = response.data;
    });
    this.getDicts("list_label").then((response) => {
      this.labelOptions = response.data;
    });
    this.getDicts("category_list").then((response) => {
      this.secondLabelOptions = response.data;
    });
  },
  methods: {
    toSets(row, ispass) {
      console.log(1111);
      let item = {
        id: row.id,
        recommendIs: 0,
      };
      if (ispass) {
        item.recommendIs = 1;
      }
      updateComprehensive_display(item).then((response) => {
        this.msgSuccess("设置完成");
        this.getList();
      });
    },
    /** 查询综合展示列表 */
    getList() {
      this.loading = true;
      listComprehensive_display(this.queryParams).then((response) => {
        this.comprehensive_displayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 对应详情类型字典翻译
    categoryFormat(row, column) {
      return this.selectDictLabel(this.categoryOptions, row.category);
    },
    // 列表标签字典翻译
    labelFormat(row, column) {
      return this.selectDictLabel(this.labelOptions, row.label);
    },
    // 分类标签字典翻译
    secondLabelFormat(row, column) {
      return this.selectDictLabel(this.secondLabelOptions, row.secondLabel);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        imagesUrl: null,
        introduction: null,
        address: null,
        recommendIs: null,
        selectedIs: null,
        category: null,
        sort: null,
        detailed: null,
        label: null,
        phoneNumber: null,
        secondLabel: null,
        consumptionPerPerson: null,
        businessHours: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加综合展示";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getComprehensive_display(id).then((response) => {
        this.form = response.data;
        if (this.form.label) {
          this.form.label = this.form.label.split(",");
        }
        if (this.form.secondLabel) {
          this.form.secondLabel = this.form.secondLabel.split(",");
        }
        this.open = true;
        this.title = "修改综合展示";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if(this.form.label){
            this.form.label = this.form.label.toString();
          }
          if(this.form.secondLabel){
            this.form.secondLabel = this.form.secondLabel.toString();
          }
          if (this.form.id != null) {
            updateComprehensive_display(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComprehensive_display(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除综合展示编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delComprehensive_display(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有综合展示数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportComprehensive_display(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>